//
//  ClubPropertyView.swift
//  77Club
//
//  Created by Apple on 2024/9/23.
//

import UIKit

class ClubPropertyTableViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .white
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubPropertyItemCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 50.0.Scale
        tableView.layer.cornerRadius = 8.0.Scale
        tableView.layer.masksToBounds = true
        return tableView
    }()

    var models: [ClubSettingModel]?{
        didSet{
            tableView.reloadData()
        }
    }
    var contentMD: ClubMinePropertyModel?{
        didSet{
            tableView.reloadData()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            tableView
        }
        
        tableView.leading(12.0.Scale).trailing(12.0.Scale).top(0).bottom(12.0.Scale)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return models?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubPropertyItemCell", for: indexPath) as! ClubPropertyItemCell
        cell.model = models?[indexPath.row]
        if let lb = contentMD?.lb, let md = models?[indexPath.row], md == .proGift {
            cell.contentLabel.text = lb
        }
        if let point = contentMD?.points, let md = models?[indexPath.row], md == .proIntegral {
            cell.contentLabel.text = String(point) 
        }
        if let order = contentMD?.order, let md = models?[indexPath.row], md == .proGoods {
            cell.contentLabel.text = order
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        guard let md = models?[indexPath.row] else { return }
        switch md {
        case .proGift:
            
            UIApplication.topController()?.navigationController?.pushViewController(ClubMyGiftViewController(), animated: true)
            break
        case .proIntegral:
            
            UIApplication.topController()?.navigationController?.pushViewController(ClubIntegralStoreViewController(), animated: true)
            break
        case .proGoods:
            
            UIApplication.topController()?.navigationController?.pushViewController(ClubExchangeDetailViewController(), animated: true)
            break
        default:
            return
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ClubPropertyItemCell: UITableViewCell {
    
    private let iconImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(15.font_medium).end()
    let contentLabel = c_make_ex(UILabel.self)
        .color("#2E3033").font(15.font_medium).alignment(.right).end()
    private let rightImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_settings_next").end()

    var model:ClubSettingModel?{
        didSet{
            guard let md = model else { return }
            iconImageV.image = md.icon.toImage
            titleLabel.text = md.title
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            iconImageV
            titleLabel
            contentLabel
            rightImageV
        }
        
        iconImageV.leading(12.0.Scale).size(24.0.Scale).centerVertically()
        rightImageV.trailing(12.0.Scale).size(20.0.Scale).centerVertically()
        titleLabel.leading(48.0.Scale).trailing(48.0.Scale)
        titleLabel.CenterY == iconImageV.CenterY
        contentLabel.trailing(34.0.Scale).centerVertically()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
